package cn.com.zcode.install.control;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

import cn.com.zcode.auth.model.Resource;
import cn.com.zcode.auth.model.UserRole;
import cn.com.zcode.auth.service.AuthService;
import cn.com.zcode.core.cache.ApplicationCacheInit;
import cn.com.zcode.core.control.ApplicationInitAware;
import cn.com.zcode.dic.model.Dictionary;
import cn.com.zcode.dic.service.DictionaryService;
import cn.com.zcode.exam.model.Paper;
import cn.com.zcode.exam.service.ExamService;
import cn.com.zcode.user.model.User;
import cn.com.zcode.user.service.UserService;

public class DefaultDataInstall implements ApplicationInitAware {
	Logger logger = Logger.getLogger(ApplicationCacheInit.class);
	private UserService userService;
	private AuthService authService;
	private DictionaryService dictionaryService;
	private ExamService examService;

	public void init(ApplicationContext context) {
		logger.info("初始化测试数据......开始");
		this.initDictionary();
		this.initUser();
		this.initAuth();
		this.initPaper();
		logger.info("初始化测试数据......完成");
	}

	public void initUser() {

		if (userService.getUserByName("admin") == null) {
			logger.info("初始化用户数据......");
			User user = new User("admin", "daemon", "admin");
			userService.saveUser(user);
		}
	}

	public void initAuth() {
		List<Resource> resources = authService.findAllResource();
		if (resources.isEmpty()) {
			logger.info("初始化权限数据......");
			Set<Resource> res = new HashSet<Resource>();
			User user = this.userService.getUserByName("admin");
			UserRole role = new UserRole("admin", "管理员");
			Set<User> users = new HashSet<User>();
			this.addResource(res, new Resource("user:invite", "user/invite.action", "authc", "邀请码"));
			this.addResource(res, new Resource("user:notice", "user/notice.action", "authc", "系统通知"));
			
			this.addResource(res, new Resource("exam:question:add", "exam/paper.action", "authc", "添加试题"));
			this.addResource(res, new Resource("exam:question:delete", "user/invite.action", "authc", "删除试题"));
			this.addResource(res, new Resource("exam:question:update", "user/invite.action", "authc", "更新试题"));

			users.add(user);
			role.setResources(res);
			role.setUsers(users);
			this.authService.saveRole(role);

		}

	}

	public void initPaper(){
		Paper paper = examService.getPaper(Paper.PAPER_WRONG_INFO);
		if(paper==null){
			User user = this.userService.getUserByName("admin");
			examService.saveWrongPaper(user);
		}
	}
	public void addResource(Set<Resource> set, Resource resource) {
		set.add(resource);
		resource.setOrderIndex(set.size() + 1);
		this.authService.saveResource(resource);
	}

	public void initDictionary() {
		List<Dictionary> dics = dictionaryService.findInitDictionary();
		if (dics.isEmpty()) {
			logger.info("初始化字典数据......");
			Dictionary pmp = new Dictionary("pmp", "PMP", null, 1, true);
			Map<String, String> map = new LinkedHashMap<String, String>();
			List<String> list = new ArrayList<String>();
			list.add("综合题目");
			list.add("整合管理");
			list.add("范围管理");
			list.add("时间管理");
			list.add("成本管理");
			list.add("质量管理");
			list.add("人力资源管理");
			list.add("沟通管理");
			list.add("风险管理");
			list.add("采购管理");
			list.add("干系人管理");
			list.add("其它");
			int num2 = 0;
			for (String string : list) {
				num2++;
				map.put("pmp_" + num2, string);
			}

			dics.add(pmp);
			dictionaryService.addDictionary(pmp);
			Set<String> set = map.keySet();
			int num = 1;
			for (String string : set) {
				Dictionary pmp1 = new Dictionary(string, map.get(string), pmp, num++, true);
				dictionaryService.addDictionary(pmp1);
				dics.add(pmp1);

			}

		}

	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public AuthService getAuthService() {
		return authService;
	}

	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}

	public ExamService getExamService() {
		return examService;
	}

	public void setExamService(ExamService examService) {
		this.examService = examService;
	}

}
